package com.example.demo.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.demo.entity.DTO.GoodsOrderSelectDTO;
import com.example.demo.entity.VO.GoodsUpLoadStatusOrderVO;
import com.example.demo.result.Result;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.example.demo.service.IGoodsOrdersService;
import com.example.demo.entity.GoodsOrders;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr.Yang
 * @since 2024-05-31
 */
@Api(tags = "商品订单接口")
@RestController
@RequestMapping("/orders")
public class GoodsOrdersController {

    @Resource
    private IGoodsOrdersService ordersService;

    /**
     * 根据id查询数据接口
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "Mr.Xiayh")
    @ApiOperation(value = "根据id查询数据接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "组件id",required = true)
    })
    //@SaCheckLogin  测试不开登录
    @GetMapping("/selectOrderByUserId")
    public Result<List<GoodsOrderSelectDTO>> findOne(@RequestParam Integer id) {
        List<GoodsOrderSelectDTO> goodsOrderSelectDTOS = ordersService.selectOrderByUserId(id);
        return Result.success(goodsOrderSelectDTOS).msg("查询成功");
    }

    @ApiOperationSupport(author = "Mr.Xiayh")
    @ApiOperation(value = "更改订单状态接口")
    @PostMapping("/updateOrderStatus")
    public Result<String> updateOrder(@RequestBody GoodsUpLoadStatusOrderVO goods) {
        if (ordersService.findById(goods.getGorderId()) == null) {
            return Result.error().code(99403).msg("订单不存在");
        }
        GoodsOrders goodsOrders = new GoodsOrders();
        BeanUtil.copyProperties(goods, goodsOrders);
        if (ordersService.updateOrderStatusById(goodsOrders)) {
            return Result.success().msg("更改成功");
        }
        return Result.error().code(99402).msg("失败");
    }

    @ApiOperationSupport(author = "Mr.Xiayh")
    @ApiOperation(value = "删除订单")
    @ApiModelProperty("订单id")
    @DeleteMapping("/deleteOrder")
    public Result<String> deleteOrder(@RequestParam Integer id) {
        if (ordersService.findById(id) == null) {
            return Result.error().code(99403).msg("订单不存在");
        }
        if (ordersService.deleteByGorderId(id)) {
            return Result.success().msg("删除成功");
        }
        return Result.error().code(99402).msg("删除失败");
    }
}